package com.world.frame;

import cn.hutool.core.util.StrUtil;
import com.world.cache.DefaultMemoryCacheOperator;
import com.world.constants.Constants;
import com.world.listener.*;
import com.world.request.dto.UserVO;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * 首页窗体
 *
 * @author WangYX
 * @version 1.0.0
 * @date 2023/11/28 16:43
 */
public class IndexFrame extends JFrame {

    private static final int DEFAULT_WIDTH = 1000;

    private static final int DEFAULT_HEIGHT = 1000;

    /**
     * 窗口名称
     */
    private static final String DEFAULT_TITLE = "world";

    /**
     * 内容区域
     */
    private final JLayeredPane contentArea = new JLayeredPane();;

    /**
     * 用户信息
     */
    private UserVO user;


    public IndexFrame() {
        //初始化用户数据
        initUserData();

        //设置窗口宽高
        this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
        //设置标题
        this.setTitle(DEFAULT_TITLE);

        //窗口居中
        Toolkit defaultToolkit = Toolkit.getDefaultToolkit();
        Dimension screenSize = defaultToolkit.getScreenSize();
        setLocation(screenSize.width / 2 - DEFAULT_WIDTH / 2, screenSize.height / 2 - DEFAULT_HEIGHT / 2);
        //图标
        Image image = defaultToolkit.getImage("src/main/resources/img/title.png");
        this.setIconImage(image);

        //退出操作
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //不可拉伸
        this.setResizable(false);
        //背景色设置为白色
        this.getContentPane().setBackground(Color.white);
        //设置布局方式为绝对布局
        this.getContentPane().setLayout(null);
        //设置窗体为可见
        this.setVisible(true);

        baseInfo();

        menuArea();

        contentArea();
    }

    /**
     * 初始化用户数据
     */
    private void initUserData() {
        user = (UserVO) DefaultMemoryCacheOperator.get(Constants.USER_KEY);
    }

    /**
     * 基础信息展示
     */
    private void baseInfo() {

        //创建具有分层的JLayeredPane
        JLayeredPane layeredPane = new JLayeredPane();
        //边框
        layeredPane.setBorder(new LineBorder(Color.WHITE));
        //大小
        layeredPane.setBounds(5, 5, 976, 200);
        //背景色设置为白色
        layeredPane.setBackground(Color.white);
        //背景图片
        baseInfoBackground(layeredPane);
        //头像
        layeredPane.add(getProfilePanel(), JLayeredPane.MODAL_LAYER);

        userName(layeredPane);

        this.getContentPane().add(layeredPane);
    }

    /**
     * 基础信息-背景图片
     */
    private void baseInfoBackground(JLayeredPane layeredPane) {

        JPanel panel = new JPanel();
        //大小
        panel.setBounds(0, -5, 976, 205);
        layeredPane.add(panel, JLayeredPane.DEFAULT_LAYER);
        //图片
        ImageIcon imageIcon = new ImageIcon();
        Image image = Toolkit.getDefaultToolkit().getImage("src/main/resources/img/index-base2.png");
        imageIcon.setImage(image);
        imageIcon.setImage(imageIcon.getImage().getScaledInstance(976, 205, Image.SCALE_DEFAULT));

        JLabel label = new JLabel("");
        label.setIcon(imageIcon);
        panel.add(label);

    }

    /**
     * 基础信息-头像框位置
     */
    private JPanel getProfilePanel() {
        //头像框
        ImageIcon profileImage = new ImageIcon();
        Image image = null;
        if (StrUtil.isNotBlank(user.getPicUrl())) {
            URL url = null;
            try {
                url = new URL(user.getPicUrl());
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            image = Toolkit.getDefaultToolkit().getImage(url);
        } else {
            image = Toolkit.getDefaultToolkit().getImage("src/main/resources/img/profile.png");
        }
        profileImage.setImage(image);
        profileImage.setImage(profileImage.getImage().getScaledInstance(100, 100, Image.SCALE_DEFAULT));

        JPanel profilePanel = new JPanel();
        //设置为透明
        profilePanel.setOpaque(false);
        //边框
        profilePanel.setBorder(new LineBorder(Color.LIGHT_GRAY));
        profilePanel.setBounds(10, 10, 100, 100);

        JLabel label = new JLabel("");
        label.setIcon(profileImage);
        profilePanel.add(label);
        return profilePanel;
    }

    /**
     * 基础信息-姓名和昵称
     */
    private void userName(JLayeredPane layeredPane) {
        JLabel name = new JLabel(user.getName());
        name.setBounds(20, 110, 100, 40);
        //字体
        Font font = new Font("微软雅黑", Font.BOLD, 20);
        name.setFont(font);
        //字体颜色
        name.setForeground(Color.YELLOW);
        layeredPane.add(name, JLayeredPane.MODAL_LAYER);

        //昵称
        JLabel nickName = new JLabel(user.getNickname());
        nickName.setBounds(20, 160, 100, 20);
        //字体
        Font font1 = new Font("微软雅黑", Font.BOLD, 15);
        nickName.setFont(font1);
        //字体颜色
        nickName.setForeground(Color.orange);
        layeredPane.add(nickName, JLayeredPane.MODAL_LAYER);
    }

    /**
     * 基础信息-展示用户信息
     */
    private void showUserInfo() {
        JPanel panel = new JPanel();
        panel.setBorder(new LineBorder(Color.gray));
        panel.setBounds(5, 5, 200, 200);
        this.getContentPane().add(panel);
    }

    /**
     * 菜单区域
     */
    private void menuArea() {
        JLayeredPane layeredPane = new JLayeredPane();
        layeredPane.setBorder(new LineBorder(Color.white));
        layeredPane.setBounds(5, 210, 150, 745);
        //背景色
        background(layeredPane);

        personInfo(layeredPane);
        equipment(layeredPane);
        family(layeredPane);
        experience(layeredPane);
        realm(layeredPane);

        this.getContentPane().add(layeredPane);
    }

    /**
     * 菜单区域-背景色
     */
    private void background(JLayeredPane layeredPane){
        JPanel panel = new JPanel();
        panel.setBounds(0,0,150,745);
        Color color = new Color(251, 248, 255);
        panel.setBackground(color);
        layeredPane.add(panel,JLayeredPane.DEFAULT_LAYER);
    }

    /**
     * 菜单区域-个人信息
     */
    private void personInfo(JLayeredPane layeredPane){
        JPanel panel = new JPanel();
        panel.setBounds(5,19,130,35);
        Color color = new Color(251, 248, 255);
        panel.setBackground(color);

        //图标
        ImageIcon userImg = new ImageIcon();
        userImg.setImage(Toolkit.getDefaultToolkit().getImage("src/main/resources/img/个人信息.png"));
        //设置图片在窗体中显示的宽度、高度
        userImg.setImage(userImg.getImage().getScaledInstance(25, 25, Image.SCALE_DEFAULT));
        JLabel label = new JLabel("");
        label.setBounds(10, 19, 30, 30);
        label.setIcon(userImg);
        panel.add(label);

        JLabel label1 = new JLabel("个人信息");
        //位置
        label1.setBounds(40, 20, 100, 30);
        Font font = new Font("微软雅黑", Font.PLAIN, 16);
        label1.setFont(font);
        //字体颜色
        label1.setForeground(Color.GRAY);

        panel.add(label1);

        layeredPane.add(panel,JLayeredPane.MODAL_LAYER);

        //添加监听
        panel.addMouseListener(new PersonInfoMouserListener(contentArea,panel));
    }

    /**
     * 菜单区域-武器装备
     */
    public void equipment(JLayeredPane layeredPane){

        JPanel panel = new JPanel();
        panel.setBounds(5,69,130,35);
        Color color = new Color(251, 248, 255);
        panel.setBackground(color);

        ImageIcon img = new ImageIcon();
        img.setImage(Toolkit.getDefaultToolkit().getImage("src/main/resources/img/knife_css.png"));
        //设置图片在窗体中显示的宽度、高度
        img.setImage(img.getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT));

        JLabel label = new JLabel("");
        label.setBounds(10, 69, 30, 30);
        label.setIcon(img);
        panel.add(label);

        JLabel label1 = new JLabel("武器装备");
        //位置
        label1.setBounds(40, 69, 100, 30);
        Font font = new Font("微软雅黑", Font.PLAIN, 16);
        label1.setFont(font);
        //字体颜色
        label1.setForeground(Color.GRAY);
        panel.add(label1);

        layeredPane.add(panel,JLayeredPane.MODAL_LAYER);

        //添加监听
        panel.addMouseListener(new EquipmentMouserListener(contentArea,panel));
    }

    /**
     * 菜单区域-家庭关系
     */
    public void family(JLayeredPane layeredPane){
        JPanel panel = new JPanel();
        panel.setBounds(5,119,130,35);
        Color color = new Color(251, 248, 255);
        panel.setBackground(color);

        ImageIcon img = new ImageIcon();
        img.setImage(Toolkit.getDefaultToolkit().getImage("src/main/resources/img/家庭管理.png"));
        //设置图片在窗体中显示的宽度、高度
        img.setImage(img.getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT));

        JLabel label = new JLabel("");
        label.setBounds(10, 119, 30, 30);
        label.setIcon(img);
        panel.add(label);

        JLabel label1 = new JLabel("家庭关系");
        //位置
        label1.setBounds(40, 119, 100, 30);
        Font font = new Font("微软雅黑", Font.PLAIN, 16);
        label1.setFont(font);
        //字体颜色
        label1.setForeground(Color.GRAY);
        panel.add(label1);

        layeredPane.add(panel,JLayeredPane.MODAL_LAYER);

        //添加监听
        panel.addMouseListener(new FamilyMouserListener(contentArea,panel));
    }


    /**
     * 菜单区域-成长经历
     */
    public void experience(JLayeredPane layeredPane){
        JPanel panel = new JPanel();
        panel.setBounds(5,169,130,35);
        Color color = new Color(251, 248, 255);
        panel.setBackground(color);

        ImageIcon img = new ImageIcon();
        img.setImage(Toolkit.getDefaultToolkit().getImage("src/main/resources/img/工作经验.png"));
        //设置图片在窗体中显示的宽度、高度
        img.setImage(img.getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT));

        JLabel label = new JLabel("");
        label.setBounds(10, 169, 30, 30);
        label.setIcon(img);
        panel.add(label);

        JLabel label1 = new JLabel("成长经历");
        //位置
        label1.setBounds(40, 169, 100, 30);
        Font font = new Font("微软雅黑", Font.PLAIN, 16);
        label1.setFont(font);
        //字体颜色
        label1.setForeground(Color.GRAY);
        panel.add(label1);

        layeredPane.add(panel,JLayeredPane.MODAL_LAYER);

        //添加监听
        panel.addMouseListener(new ExperienceMouserListener(contentArea,panel));
    }

    /**
     * 菜单区域-境界管理
     */
    public void realm(JLayeredPane layeredPane){
        JPanel panel = new JPanel();
        panel.setBounds(5,219,130,35);
        Color color = new Color(251, 248, 255);
        panel.setBackground(color);

        ImageIcon img = new ImageIcon();
        img.setImage(Toolkit.getDefaultToolkit().getImage("src/main/resources/img/图层.png"));
        //设置图片在窗体中显示的宽度、高度
        img.setImage(img.getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT));

        JLabel label = new JLabel("");
        label.setBounds(10, 219, 30, 30);
        label.setIcon(img);
        panel.add(label);

        JLabel label1 = new JLabel("境界管理");
        //位置
        label1.setBounds(40, 219, 100, 30);
        Font font = new Font("微软雅黑", Font.PLAIN, 16);
        label1.setFont(font);
        //字体颜色
        label1.setForeground(Color.GRAY);
        panel.add(label1);

        layeredPane.add(panel,JLayeredPane.MODAL_LAYER);

        //添加监听
        panel.addMouseListener(new RealmMouseListener(contentArea,panel));
    }

    /**
     * 内容区域
     */
    private void contentArea() {
        contentArea.setBorder(new LineBorder(Color.gray));
        contentArea.setBounds(160, 210, 821, 745);
        this.getContentPane().add(contentArea);
    }


    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            new IndexFrame();
        });
    }

}
